package com.example.test_tab;

import android.content.Context;
import android.graphics.Canvas;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

public class VerticalTextView extends TextView{
	   final boolean topDown;
	   int angle = 90;
	   Context con;

	   public VerticalTextView(Context context, AttributeSet attrs){
	      super(context, attrs);
	      
	      con = context;
	      final int gravity = getGravity();
	      if(Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
	    	  
	         setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
	         topDown = false;
	      }else{
	         topDown = true;
	      }
	      
	      //topDown = true;
	      
	      for(int i=0; i< attrs.getAttributeCount();i++){
	    	  String attrsname = attrs.getAttributeName(i);
	    	  if(attrsname.equals("angle")){
	    		  angle = attrs.getAttributeIntValue(i, 0);
	    		  MunLog.show("angle:" + angle);
	    	  }
	    	  
	      }
	      
	   }

	   @Override
	   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
	      super.onMeasure(heightMeasureSpec, widthMeasureSpec);
	      setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
	   }

	   @Override
	   protected void onDraw(Canvas canvas){
	      TextPaint textPaint = getPaint(); 
	      textPaint.setColor(getCurrentTextColor());
	      textPaint.drawableState = getDrawableState();

	      canvas.save();
	      

	      if(topDown == true){
	         canvas.translate(getWidth(), 0);
	         canvas.rotate(90);//angle
	      }else {
	         canvas.translate(0, getHeight());
	         canvas.rotate(-90);//angle
	      }

	      

	      canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

	      getLayout().draw(canvas);
	      canvas.restore();
	      
	      
	  }
	}
